Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve typing-time lazy import behaviors to expose attribute errors to type checkers #1052

Merged
merged 3 commits into from
Sep 17, 2024

Conversation

sirosen
Copy link
Member

@sirosen sirosen commented Sep 17, 2024

Basically, the three commits are:

  • Run "tox -m freezedeps" to ensure we're on the latest mypy version
  • Add a failing typing test
  • Fix the lazy-importer at typing-time

📚 Documentation preview 📚: https://globus-sdk-python--1052.org.readthedocs.build/en/1052/

Ensure that a missing name gets a type-error, meaning that the getattr
importer hasn't obscured other errors.
Move the definitions of `__getattr__` and `__dir__` into the `else`
block of the `if TYPE_CHECKING` directive, meaning that they are not
evaluated at typing time.

Doing so forces `mypy` to evaluate the listed imports as though they
were the only things in the module, and to treat them and the
`__all__` list as a canonical listing of top-level `globus_sdk`
attributes. As a result, previously undetectable errors (such as a bad
module-attribute name) are properly detected.

Without this characteristic, `mypy` sees the `__getattr__` method and
assumes that any module attribute access will be valid.
@sirosen sirosen merged commit d76a594 into globus:main Sep 17, 2024
16 checks passed
@sirosen sirosen deleted the improve-lazy-import-typing branch September 17, 2024 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants